package com.example.demo.thread;

/**
 * 线程的等待唤醒通知await和signal必须持有锁，必须在锁块synchronize和lock中
 * 必须要先等待后唤醒，线程才能够被唤醒
 */
public class WaitNotifyDemo {

    public static void main(String[] args) {
        Object objectLock = new Object();
        new Thread(() -> {
            synchronized (objectLock) {
                System.out.println(Thread.currentThread().getName() + "------come in");
                try {
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "-------被唤醒");
        }, "A").start();

        new Thread(() -> {
            synchronized (objectLock) {
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "-------通知");
            }
        }, "B").start();

    }

}
